package com.hxc.interest.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.lang.Assert;
import com.hxc.interest.domain.TNoticeRead;
import com.hxc.interest.mapper.TNoticeReadMapper;
import com.hxc.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxc.interest.mapper.TNoticeInfoMapper;
import com.hxc.interest.domain.TNoticeInfo;
import com.hxc.interest.service.ITNoticeInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知公告信息Service业务层处理
 * 
 * @author hxc
 * @date 2021-08-16
 */
@Service
public class TNoticeInfoServiceImpl implements ITNoticeInfoService 
{
    @Autowired
    private TNoticeInfoMapper tNoticeInfoMapper;


    @Autowired
    private TNoticeReadMapper tNoticeReadMapper;

    /**
     * 查询通知公告信息
     * 
     * @param id 通知公告信息主键
     * @return 通知公告信息
     */
    @Override
    public TNoticeInfo selectTNoticeInfoById(Long id)
    {
        return tNoticeInfoMapper.selectTNoticeInfoById(id);
    }

    /**
     * 查询通知公告信息列表
     * 
     * @param tNoticeInfo 通知公告信息
     * @return 通知公告信息
     */
    @Override
    public List<TNoticeInfo> selectTNoticeInfoList(TNoticeInfo tNoticeInfo)
    {
        List<TNoticeInfo> list = tNoticeInfoMapper.selectTNoticeInfoList(tNoticeInfo);
        return list;
    }

    /**
     * 新增通知公告信息
     * 
     * @param tNoticeInfo 通知公告信息
     * @return 结果
     */
    @Override
    public int insertTNoticeInfo(TNoticeInfo tNoticeInfo)
    {
        //默认阅读量
        tNoticeInfo.setReadNum(0);
        tNoticeInfo.setCreateTime(DateUtils.getNowDate());
        return tNoticeInfoMapper.insertTNoticeInfo(tNoticeInfo);
    }

    /**
     * 修改通知公告信息
     * 
     * @param tNoticeInfo 通知公告信息
     * @return 结果
     */
    @Override
    public int updateTNoticeInfo(TNoticeInfo tNoticeInfo)
    {
        return tNoticeInfoMapper.updateTNoticeInfo(tNoticeInfo);
    }

    /**
     * 批量删除通知公告信息
     * 
     * @param ids 需要删除的通知公告信息主键
     * @return 结果
     */
    @Override
    public int deleteTNoticeInfoByIds(Long[] ids)
    {
        return tNoticeInfoMapper.deleteTNoticeInfoByIds(ids);
    }

    /**
     * 删除通知公告信息信息
     * 
     * @param id 通知公告信息主键
     * @return 结果
     */
    @Override
    public int deleteTNoticeInfoById(Long id)
    {
        return tNoticeInfoMapper.deleteTNoticeInfoById(id);
    }


    @Override
    @Transactional
    public int editReadNum(TNoticeRead tNoticeRead) {
        Assert.notNull(tNoticeRead,"noticeId 不能为空");
        int read = 0;
        List<TNoticeRead> noticeReadList = tNoticeReadMapper.selectTNoticeReadList(tNoticeRead);
        if (noticeReadList.size() > 0){
            //保存阅读用户信息
            tNoticeRead.setCreateTime(new Date());
            tNoticeReadMapper.insertTNoticeRead(tNoticeRead);
            //修改阅读量
            read = tNoticeInfoMapper.editReadNum(tNoticeRead.getUserId().intValue());
        }
        return read;
    }

    @Override
    public List<TNoticeInfo> queryMineNitoceList(TNoticeInfo tNoticeInfo) {
        Assert.notNull(tNoticeInfo.getUserId(),"userId不能为空");
        Assert.notNull(tNoticeInfo.getNoticeType(),"noticeType不能为空");
        List<TNoticeInfo> tNoticeInfos = tNoticeInfoMapper.queryMineNitoceList(tNoticeInfo);
        return tNoticeInfos;
    }

    @Override
    public List<TNoticeInfo> queryNitoceList(TNoticeInfo tNoticeInfo) {
        List<TNoticeInfo> noticeInfoList = tNoticeInfoMapper.queryNitoceList(tNoticeInfo);
        if(noticeInfoList.size() > 0){
            noticeInfoList.forEach((noticeInfo)->{
                //查询用户是否已读公告
                TNoticeRead queryreaders = tNoticeReadMapper.queryreaders(noticeInfo.getId(), tNoticeInfo.getUserId());
                if (ObjectUtils.isNotEmpty(queryreaders)){
                    noticeInfo.setRead(true);
                }
            });
        }
        return noticeInfoList;
    }

}
